package android.zhcs.myapplication;

import androidx.annotation.NonNull;
import androidx.appcompat.app.AppCompatActivity;
import androidx.fragment.app.Fragment;
import androidx.viewpager2.adapter.FragmentStateAdapter;
import androidx.viewpager2.widget.CompositePageTransformer;
import androidx.viewpager2.widget.ViewPager2;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.zhcs.myapplication.ui.dashboard.DashboardFragment;

import org.jetbrains.annotations.NotNull;

import java.util.Timer;
import java.util.TimerTask;

public class mmmMainActivity2 extends AppCompatActivity {

    private ViewPager2 viewPager2;
    private int nowTag;

    public static void start(Context context){
        Intent intent = new Intent(context, mmmMainActivity2.class);
        context.startActivity(intent);
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        getSupportActionBar().hide();
        setContentView(R.layout.activity_mmm_main2);
        viewPager2 = findViewById(R.id.viewp);
        viewPager2.setAdapter(new FragmentStateAdapter(this) {
            @NonNull
            @NotNull
            @Override
            public Fragment createFragment(int position) {
                return new DashboardFragment(position);
            }

            @Override
            public int getItemCount() {
                return 2;
            }
        });
        viewPager2.setOffscreenPageLimit(99);
        viewPager2.setPageTransformer(new CompositePageTransformer());
        viewPager2.registerOnPageChangeCallback(new ViewPager2.OnPageChangeCallback() {
            @Override
            public void onPageSelected(int position) {
                nowTag = position;
                super.onPageSelected(position);
            }
        });
        Timer timer = new Timer();
       TimerTask timerTask = new TimerTask(){
            @Override
            public void run() {
                runOnUiThread(new Runnable() {
                    @Override
                    public void run() {
                        viewPager2.setCurrentItem(nowTag++);
                        if(nowTag == 3) nowTag = 0;
                    }
                });
            }
        };
        timer.schedule(timerTask,0,800);
    }
}